package com.ibm.ba.demo.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ibm.ba.demo.dao.ProductDao;
import com.ibm.ba.demo.entity.BaDemoScoreResult;
import com.ibm.ba.demo.entity.Product;
import com.ibm.ba.demo.entity.SPSSModelConfiguration;
import com.ibm.ba.demo.exception.GeneralException;
import com.ibm.ba.demo.helper.BeanHelper;
import com.spss.scoring.schema.v2.jaxb.Input;
import com.spss.scoring.schema.v2.jaxb.RequestInputRow;
import com.spss.scoring.schema.v2.jaxb.RowValues;
import com.spss.scoring.schema.v2.jaxb.ScoreRequest;
import com.spss.scoring.schema.v2.jaxb.ScoreResult;
import com.spss.scoring.schema.v2.jaxb.Value;

@Service
public class ExDBScoreResultServiceImpl implements ScoreResultService {

	private static Logger logger = Logger
			.getLogger(ExDBScoreResultServiceImpl.class);

	@Autowired
	private SPSSModelConfiguration modelConfig;
	
	@Autowired
	ProductDao productDao;

	@Override
	public List<BaDemoScoreResult> convert(ScoreRequest scoreRequest,
			ScoreResult scoreResult) throws GeneralException {
		List<BaDemoScoreResult> baDemoScoreResultList = new ArrayList<BaDemoScoreResult>();

		for (int i = 0; i < scoreRequest.getRequestInputTable().get(0)
				.getRequestInputRow().size(); i++) {
			RequestInputRow requestInputRow = scoreRequest
					.getRequestInputTable().get(0).getRequestInputRow().get(i);
			BaDemoScoreResult baDemoScoreResult = new BaDemoScoreResult();
			for (Input input : requestInputRow.getInput()) {
				if (input.getName()
						.equalsIgnoreCase(modelConfig.getCustField())) {
					baDemoScoreResult.setCustomerId(Integer.parseInt(input
							.getValue()));
					break;
				}

			}
			RowValues rowValues = scoreResult.getRowValues().get(i);

			List<Product> prodList = new ArrayList<Product>();
			List<Value> valueList = rowValues.getValue();
			Product product = new Product();
			product.setProductId(Integer.parseInt(valueList.get(0).getValue()
					.replaceAll(modelConfig.getSpssProductPrefix(), "")));

			product.setProductScore(valueList.get(1).getValue());
			prodList.add(product);

			product = new Product();
			product.setProductId(Integer.parseInt(valueList.get(3).getValue()
					.replaceAll(modelConfig.getSpssProductPrefix(), "")));

			product.setProductScore(valueList.get(4).getValue());
			prodList.add(product);

			product = new Product();
			product.setProductId(Integer.parseInt(valueList.get(6).getValue()
					.replaceAll(modelConfig.getSpssProductPrefix(), "")));

			product.setProductScore(valueList.get(7).getValue());
			prodList.add(product);
			baDemoScoreResult.setProducts(productDao.getProductNames(prodList));
			baDemoScoreResultList.add(baDemoScoreResult);
		}
		logger.info("baDemoScoreResultList is: "
				+ BeanHelper.beanToJson(baDemoScoreResultList));
		return baDemoScoreResultList;
	}

}
